package com.gykfbxxh.imovie.view.main.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.gykfbxxh.imovie.R;
import com.gykfbxxh.imovie.adapter.NewsFragmentPagerAdapter;
import com.gykfbxxh.imovie.base.BaseFragment;
import com.gykfbxxh.imovie.custom.view.SimpleViewPagerIndicator;
import com.gykfbxxh.imovie.view.main.activity.LikeNoLikeActivity;
import com.gykfbxxh.imovie.view.set.SearchActivity;
import com.xiao.nicevideoplayer.NiceVideoPlayerManager;

import java.util.ArrayList;

/**
 * Created by xuxinghai
 * On 2017/8/30
 * 推荐fragment
 */

public class RecommendFragment extends BaseFragment implements ViewPager.OnPageChangeListener, View.OnClickListener {
    private Activity mActivity;
    private ViewGroup mContentView;//根布局
    private ImageView iv_recommend, iv_search, iv_logo;
    private SimpleViewPagerIndicator simpleIndicator;
    private ViewPager mViewPager;
    private ArrayList<Fragment> fragments = new ArrayList<Fragment>();
    private String[] titles = new String[]{"推荐", "片花"};


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onAttach(Activity activity) {
        mActivity = activity;
        super.onAttach(activity);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (mContentView == null) {
            mContentView = (ViewGroup) inflater.inflate(
                    R.layout.fragment_recommend_page, null);
            initView();
        }


        return mContentView;
    }

    /**
     * 初始化layout控件
     */
    private void initView() {
        iv_recommend = (ImageView) mContentView.findViewById(R.id.iv_recommend);
        iv_search = (ImageView) mContentView.findViewById(R.id.iv_search);
        iv_logo = (ImageView) mContentView.findViewById(R.id.iv_logo);
        iv_search.setOnClickListener(this);
        iv_recommend.setOnClickListener(this);
        simpleIndicator = (SimpleViewPagerIndicator) mContentView.findViewById(R.id.simpleIndicator);
        mViewPager = (ViewPager) mContentView.findViewById(R.id.mViewPager);
        simpleIndicator.setOnIndicatorTitleClick(new SimpleViewPagerIndicator.IndicatorTitleClick() {
            @Override
            public void onClick(int position) {
                mViewPager.setCurrentItem(position);
            }
        });
        initFragments();
    }

    private void initFragments() {
        for (int i = 1; i < 3; i++) {
            Bundle data = new Bundle();
            data.putInt("type", i );
            FilmsListFragment filmsFragment = new FilmsListFragment();
            filmsFragment.setArguments(data);
            fragments.add(filmsFragment);
        }

//        FilmsListFragment filmsFragment1 = new FilmsListFragment();
//        FilmsListFragment filmsFragment2 = new FilmsListFragment();
//        FilmsListFragment filmsFragment3 = new FilmsListFragment();
//        fragments.add(filmsFragment1);
//        fragments.add(filmsFragment2);
//        fragments.add(filmsFragment3);
        NewsFragmentPagerAdapter mAdapter = new NewsFragmentPagerAdapter(getChildFragmentManager(), fragments);
        mViewPager.setAdapter(mAdapter);
        mViewPager.setCurrentItem(0);
        mViewPager.addOnPageChangeListener(this);
        simpleIndicator.setmTextSize(14);
        simpleIndicator.setTitles(titles);
        simpleIndicator.pageSelect(0);
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        simpleIndicator.scroll(position, positionOffset);
    }

    @Override
    public void onPageSelected(int position) {
        simpleIndicator.pageSelect(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_search://搜索按钮
                startActivity(new Intent(mActivity, SearchActivity.class));
                break;
            case R.id.iv_recommend://今日推荐
                startActivity(new Intent(mActivity, LikeNoLikeActivity.class));
                break;
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        NiceVideoPlayerManager.instance().releaseNiceVideoPlayer();
    }
}
